<?php

class MailUtil
{

    private $SMTP_HOST = 'smtp.gmail.com';
    private $SMTP_PORT = 465;
    private $MAIL_ACC = 'choilodenao@gmail.com';
    private $MAIL_PASS = 'khanhlode12345';
    private $LODE_MAIL = 'lathudau_d@yahoo.com';
    private $LODE_MAIL_DISPLAY_NAME = "Lô Đề";
    private $WELCOME_SUBJECT = 'Chào mừng đến thế giới Lô Đề. Thông tin tài khoản tại LoDe.Net';
    private $ACTIVE_SUBJECT = 'Kích hoạt tài khoản LoDe.Net';
    private $RESET_SUBJECT = 'Reset mật khẩu tài khoản tại LoDe.Net';
    private $CHAR_SET = 'utf-8';
    private $MAIL_TEMPLATE_PATH = 'mail_template/';
    private $WELCOME_MAIL_TEMPLATE = 'welcome_template.php';
    private $ACTIVE_MAIL_TEMPLATE = 'active_template.php';
    private $RESET_MAIL_TEMPLATE = 'reset_template.php';

    const ACTIVE_FLAG = 0;
    const RESET_FLAG = 1;

    const BASE_URL = 'http://lode.vocit.net/index.php';

    private $mailer;

    function __construct()
    {
        $this->mailer = new PHPMailer();

        $this->mailer->IsSMTP();
        $this->mailer->IsHTML();
        $this->mailer->SMTPDebug = 1;
        $this->mailer->SMTPAuth = TRUE;
        $this->mailer->SMTPSecure = 'ssl';
        $this->mailer->Host = $this->SMTP_HOST;
        $this->mailer->Port = $this->SMTP_PORT;
        $this->mailer->Username = $this->MAIL_ACC;
        $this->mailer->Password = $this->MAIL_PASS;

        $this->mailer->CharSet = $this->CHAR_SET;
        $this->mailer->SetFrom($this->LODE_MAIL, $this->LODE_MAIL_DISPLAY_NAME);
    }

    public function sendWelcomeMail($user)
    {
        $this->mailer->Subject = $this->WELCOME_SUBJECT;
        $this->mailer->AddAddress($user->email, $user->your_name);
        $body = $this->compile(array('user' => $user), $this->WELCOME_MAIL_TEMPLATE);
        $this->mailer->Body = $body;
        if (!$this->mailer->Send())
        {
            Logger::writeLog('Fail to send welcome mail. user info'.print_r($user, TRUE), Logger::ERROR_LEVEL, 'MailUtil');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

    public function sendActiveMail($user)
    {
        $this->mailer->Subject = $this->ACTIVE_SUBJECT;
        $this->mailer->AddAddress($user->email, $user->your_name);

        $validToken = new ValidToken();
        $validToken->user_id = $user->user_id;
        $validToken->valid_time = date('Y-m-d H:i:s', strtotime('1 day'));
        $validToken->token = sha1($validToken->user_id.$validToken->valid_time);
        $validToken->active_reset = self::ACTIVE_FLAG;

        if ($validToken->save())
        {
            $activeLink = self::BASE_URL.'/user/active?token='.$validToken->token;
            $body = $this->compile(array('user' => $user, 'link' => $activeLink), $this->ACTIVE_MAIL_TEMPLATE);
            $this->mailer->Body = $body;
            if (!$this->mailer->Send())
            {
                Logger::writeLog('Fail to send active mail. user info'.print_r($user, TRUE), Logger::ERROR_LEVEL, 'MailUtil');
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }
        else
        {
            return FALSE;
        }
    }

    public function sendResetMail($user)
    {
        $this->mailer->Subject = $this->RESET_SUBJECT;
        $this->mailer->AddAddress($user->email, $user->your_name);

        $validToken = new ValidToken();
        $validToken->user_id = $user->user_id;
        $validToken->valid_time = date('Y-m-d H:i:s', strtotime('1 day'));
        $validToken->token = sha1($validToken->user_id.$validToken->valid_time);
        $validToken->active_reset = self::RESET_FLAG;

        if ($validToken->save())
        {

            $info['name'] = $user->your_name;
            $info['link'] = self::BASE_URL.'/user/reset?token='.$validToken->token;
            $body = $this->compile($info, $this->RESET_MAIL_TEMPLATE);
            $this->mailer->Body = $body;
            if (!$this->mailer->Send())
            {
                Logger::writeLog('Fail to send reset mail. user info'.print_r($user, TRUE), Logger::ERROR_LEVEL, 'MailUtil');
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }
        else
        {
            return FALSE;
        }
    }

    public function compile($info, $templateFileName)
    {
        ob_start();
        extract($info);
        include $this->MAIL_TEMPLATE_PATH.$templateFileName;
        $content = ob_get_contents();
        ob_end_clean();

        return $content;
    }

}

?>
